<?php
namespace app\integral\controller;
use app\AdminRest;


use app\ApiRest;
use app\banquet\model\AtvContent;
use app\banquet\model\Config;
use app\banquet\model\Evaluate;
use app\banquet\model\Order;
use app\banquet\model\OrderContent;
use app\banquet\model\Package;
use app\banquet\model\Place;
use app\banquet\model\PlaceDate;
use app\banquet\model\PlaceIv;
use app\banquet\model\Refund;
use app\card\model\Company;
use app\card\model\User;
use app\card\model\UserInfo;
use app\integral\model\Integral;
use app\integral\model\IntegralGoods;
use app\member\model\Member;
use app\shop\model\IndexGoods;
use app\shop\model\IndexShopType;
use app\shop\model\IndexUserInfo;
use app\shop\model\IndexUserShop;
use think\App;
use think\facade\Db;


class IndexAtv extends ApiRest
{

    protected $model;

    protected $date_model;

    protected $package_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new Integral();

        $this->model->initAtv();

//        $this->date_model  = new PlaceDate();
//
//        $this->package_model  = new Package();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-12-23 16:19
     * @功能说明:积分活动商品列表
     */
    public function index(){

        $input  = $this->_param;

        $to_uid = $input['staff_id'];

        $user_info_model = new IndexUserInfo();
        //公司id
        $company_id = $user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status' ,'=',1];

        $dis[] = ['a.atv_status' ,'=',2];

        $coupon_dis = $dis;

        $dis[] = ['a.goods_type' ,'=',1];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['a.name','like',"%".$input['name']."%"];
        }
        //公共商品
        $where[] = ['b.public_goods','=',1];
        //在这个公司下面
        $where[] = ['c.company_id' ,'=',$company_id];

        $goods  = $this->model->companyGoodsList($dis,$where,5);
        //商品名字搜索
        if(!empty($input['name'])){

            $coupon_dis[] = ['b.title','like',"%".$input['name']."%"];
        }

        $coupon_dis[] = ['a.goods_type' ,'=',2];
        //公共商品
        $where1[] = ['b.company_limit','=',0];
        //在这个公司下面
        $where1[] = ['c.company_id' ,'=',$company_id];

        $coupon = $this->model->companyCouponList($coupon_dis,$where1,5);
        //兑换商品和优惠券列表 10条
        $data['list'] = array_merge($goods['data'],$coupon['data']);

        if(!empty($data['list'])){

            $i_model = new IntegralGoods();

            foreach ($data['list'] as &$v){

                if($v['goods_type']==1){

                    $v['goods_price'] = !empty($v['goods_info'][0]['goods_price'])?$v['goods_info'][0]['goods_price']:$v['goods_price'];
                }

                $info = $i_model->dataInfo(['atv_id'=>$v['id']]);

                $v['i_price']    = !empty($info['price'])?$info['price']:0;

                $v['i_integral'] = !empty($info['integral'])?$info['integral']:0;
            }

        }

        $member_model = new Member();

        $member = $member_model->memberUpdateInfo(['user_id' => $this->getUserId(),'uniacid'=>$this->_uniacid]);

        $data['integral'] = $member['integral'];

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-20 14:44
     * @功能说明:活动商品列表
     */
    public function atvGoodsList(){

        $input  = $this->_param;

        $to_uid = $input['staff_id'];

        $user_info_model = new IndexUserInfo();

        $shop_type_model = new IndexShopType();
        //公司id
        $company_id = $user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status' ,'=',1];

        $dis[] = ['a.atv_status' ,'=',2];

        $dis[] = ['a.goods_type' ,'=',1];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['b.name','like',"%".$input['name']."%"];
        }

        if(!empty($input['cate_id'])){

            $all_cat = $shop_type_model->getSonCatId($input['cate_id']);

            $dis[]   = ['b.type','in',$all_cat];
        }
        //公共商品
        $where[] = ['b.public_goods','=',1];
        //在这个公司下面
        $where[] = ['c.company_id' ,'=',$company_id];

        $goods  = $this->model->companyGoodsList($dis,$where,10);

        if(!empty($goods['data'])){

            $i_model = new IntegralGoods();

            foreach ($goods['data'] as &$v){

                $v['goods_price'] = !empty($v['goods_info'][0]['goods_price'])?$v['goods_info'][0]['goods_price']:$v['goods_price'];

                $info = $i_model->dataInfo(['atv_id'=>$v['id']]);

                $v['i_price']    = !empty($info['price'])?$info['price']:0;

                $v['i_integral'] = !empty($info['integral'])?$info['integral']:0;
            }

        }

        return $this->success($goods);
    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-20 14:44
     * @功能说明:活动卡券列表
     */
    public function atvCouponList(){

        $input  = $this->_param;

        $to_uid = $input['staff_id'];

        $user_info_model = new IndexUserInfo();
        //公司id
        $company_id = $user_info_model->getCompanyId(['fans_id'=>$to_uid],$this->_uniacid);

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.status' ,'=',1];

        $dis[] = ['a.atv_status' ,'=',2];

        $dis[] = ['a.goods_type' ,'=',2];
        //商品名字搜索
        if(!empty($input['name'])){

            $dis[] = ['b.title','like',"%".$input['name']."%"];
        }
        //公共商品
        $where[] = ['b.company_limit','=',0];
        //在这个公司下面
        $where[] = ['c.company_id' ,'=',$company_id];

        $goods  = $this->model->companyCouponList($dis,$where,10);

        if(!empty($goods['data'])){

            $i_model = new IntegralGoods();

            foreach ($goods['data'] as &$v){

                $info = $i_model->dataInfo(['atv_id'=>$v['id']]);

                $v['i_price']    = !empty($info['price'])?$info['price']:0;

                $v['i_integral'] = !empty($info['integral'])?$info['integral']:0;
            }

        }

        return $this->success($goods);
    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-20 14:53
     * @功能说明:积分兑换明细
     */
    public function integralExchangeList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['status','=',2];

        $i_model = new \app\member\model\Integral();

        $data = $i_model->integralList($dis,15);

        return $this->success($data);

    }



}
